package com.example.demo.information.service.impl;

import com.example.demo.equipment.dao.CollectingDevice;
import com.example.demo.equipment.mapper.CollectingDeviceMapper;
import com.example.demo.information.dao.DataReport;
import com.example.demo.information.dao.Information;
import com.example.demo.information.dao.InformationVo;
import com.example.demo.information.dao.LineChartVo;
import com.example.demo.information.mapper.InformationMapper;
import com.example.demo.information.service.InformationService;
import com.example.demo.system.utility.PageInfo;
import com.example.demo.system.utility.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class InformationServiceImpl implements InformationService {

    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private CollectingDeviceMapper collectingDeviceMapper;

    /**
     * 数据采集模块
     * 查询每个设备的最新一条数据
     */
    @Override
    public Result getList(Integer pageNum, Integer pageSize, String name) {
        //计算开始条数
        Integer start = PageInfo.start(pageNum, pageSize);
        //查询数据集合
        List<Information> list = informationMapper.getList(start, pageSize, name);
        ///查询总条数
        Integer listCount = informationMapper.getListCount(name);
        //封装
        PageInfo info = new PageInfo(pageNum, pageSize, listCount, list);
        return Result.success(info);
    }

    /**
     * 数据采集模块
     * 查询单个设备的数据集合
     */
    @Override
    public Result getPage(Integer pageNum, Integer pageSize, String deviceId, String startTime, String endTime) {
        //获取设备详情
        CollectingDevice device = collectingDeviceMapper.get(Integer.valueOf(deviceId));
        //计算开始条数
        Integer start = PageInfo.start(pageNum, pageSize);
        //获取采集数据列表
        List<Information> list = informationMapper.getPage(start, pageSize, deviceId,startTime,endTime);
        ///查询总条数
        Integer listCount = informationMapper.getPageCount(deviceId,startTime,endTime);
        //封装
        PageInfo info = new PageInfo(pageNum, pageSize, listCount, list);

        InformationVo vo = new InformationVo();
        vo.setDeviceId(device.getId());
        vo.setDeviceName(device.getDeviceName());
        vo.setDeviceRemark(device.getDeviceRemark());
        vo.setDataList(info);
        return Result.success(vo);
    }

    /**
     * 数据采集模块
     * 查询单个设备的数据集合
     * 获取折线图数据
     */
    @Override
    public Result getLineChart(String deviceId) {
        List<Information> lineChart = informationMapper.getLineChart(deviceId);
        LineChartVo vo = new LineChartVo();
        vo.setJcqdz1(lineChart.stream().map(Information::getJcqdz1).collect(Collectors.toList()));
        vo.setJcqdz2(lineChart.stream().map(Information::getJcqdz2).collect(Collectors.toList()));
        vo.setJcqdz3(lineChart.stream().map(Information::getJcqdz3).collect(Collectors.toList()));
        vo.setJcqdz4(lineChart.stream().map(Information::getJcqdz4).collect(Collectors.toList()));
        vo.setJcqdz5(lineChart.stream().map(Information::getJcqdz5).collect(Collectors.toList()));
        vo.setJcqdz6(lineChart.stream().map(Information::getJcqdz6).collect(Collectors.toList()));
        vo.setJcqdz7(lineChart.stream().map(Information::getJcqdz7).collect(Collectors.toList()));
        vo.setJcqdz8(lineChart.stream().map(Information::getJcqdz8).collect(Collectors.toList()));
        vo.setJcqdz9(lineChart.stream().map(Information::getJcqdz9).collect(Collectors.toList()));
        vo.setJcqdz10(lineChart.stream().map(Information::getJcqdz10).collect(Collectors.toList()));
        vo.setJcqdz11(lineChart.stream().map(Information::getJcqdz11).collect(Collectors.toList()));
        vo.setJcqdz12(lineChart.stream().map(Information::getJcqdz12).collect(Collectors.toList()));
        vo.setJcqdz13(lineChart.stream().map(Information::getJcqdz13).collect(Collectors.toList()));
        vo.setJcqdz14(lineChart.stream().map(Information::getJcqdz14).collect(Collectors.toList()));
        vo.setJcqdz15(lineChart.stream().map(Information::getJcqdz15).collect(Collectors.toList()));
        vo.setCreateTime(lineChart.stream().map(Information::getCreateTime).collect(Collectors.toList()));
        return Result.success(vo);
    }

    //数据报表模块-采集数据
    @Override
    public Result getDataList(Integer pageNum, Integer pageSize, Integer spaceId, String startTime, String endTime) {
        //计算开始条数
        Integer start = PageInfo.start(pageNum, pageSize);
        //查询数据集合
        List<Information> list = informationMapper.getDataList(start, pageSize, spaceId, startTime, endTime);
        ///查询总条数
        Integer listCount = informationMapper.getDataListCount(spaceId, startTime, endTime);
        //封装
        PageInfo info = new PageInfo(pageNum, pageSize, listCount, list);
        return Result.success(info);
    }

    //数据报表模块-生成报告
    @Override
    public Result addReport(Integer spaceId, String startTime, String endTime) {
        //查询数据集合
        List<Information> list = informationMapper.getDataList(null, null, spaceId, startTime, endTime);
        //保存报告数据
        DataReport dataReport = new DataReport();
        dataReport.setSpaceId(spaceId);
        dataReport.setStartTime(startTime);
        dataReport.setEndTime(endTime);
        dataReport.setReportTime(new Date());
        List<Integer> ids = list.stream().map(Information::getId).collect(Collectors.toList());
        dataReport.setDataList(String.valueOf(ids));
        informationMapper.addReport(dataReport);
        return Result.success("报告生成成功");
    }

    //数据报表模块-报告列表
    @Override
    public Result getReportList(Integer pageNum, Integer pageSize, Integer spaceId, String startTime, String endTime) {
        //计算开始条数
        Integer start = PageInfo.start(pageNum, pageSize);
        //查询数据集合

        List<DataReport> list = informationMapper.getReportList(start, pageSize, spaceId, startTime, endTime);
        ///查询总条数
        Integer listCount = informationMapper.getReportListCount(spaceId, startTime, endTime);
        //封装
        PageInfo info = new PageInfo(pageNum, pageSize, listCount, list);
        return Result.success(info);
    }

    @Override
    public Result deleteReport(Integer id) {
        informationMapper.deleteReport(id);
        return Result.success("删除成功");
    }


}
